package com.skydroid.demo.base

import android.net.LocalSocket
import android.net.LocalSocketAddress
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.annotation.RequiresApi
import com.skydroid.demo.common.Constants
import com.skydroid.demo.utils.BusinessUtils
import com.skydroid.demo.utils.LogUtils
import com.skydroid.demo.utils.String2ByteArrayUtils
import com.skydroid.demo.utils.ToastShow
import java.io.IOException
import java.nio.ByteBuffer
import java.util.concurrent.Executors

/**
 * author:CQD

 * e-mail:634897993@qq.com

 * data:  2020/4/25

 * desc:
 *
 * version:
 */
@RequiresApi(Build.VERSION_CODES.KITKAT)
abstract class BaseSocketSerialActivity:BaseActivity() {
    protected var localSocket: LocalSocket? = null

    protected var isConnect:Boolean = false

    protected var mReadBuffer = ByteBuffer.allocate(4096)

    protected var bindCode:String? = null

    protected var connectCode:String? = null

    val mainHanlder: Handler by lazy { Handler(Looper.getMainLooper()) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        connectDevice()
    }

    fun connectDevice() {
        createSocket()
    }

    /**
     * 创建
     */


    protected fun createSocket() {
        try {
            localSocket = LocalSocket(LocalSocket.SOCKET_DGRAM)
            bindCode?.takeIf {
                it.isNotBlank()
            }?.let {
                localSocket!!.bind(LocalSocketAddress(bindCode))
            }

            connectCode?.takeIf {
                it.isNotBlank()
            }?.let {
                localSocket!!.connect(LocalSocketAddress(connectCode))
            }

            isConnect = localSocket!!.isConnected
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
        read()
    }

    /**
     * 开始读
     */
    open fun read() {
        Thread(Runnable {
            try {
                while (true) {
                    val inputStream = localSocket!!.inputStream
                    val len = inputStream.read(mReadBuffer.array())
                    if (len > 0) {
                        val data = ByteArray(len)
                        mReadBuffer[data, 0, len]
                        mainHanlder.post(

                            Runnable {
                                receive(data)

                            })
                        mReadBuffer.clear()
                    }
                }
            } catch (e: java.lang.Exception) {
                e.printStackTrace()
            }
        }).start()
    }


    protected abstract fun receive(datas:ByteArray?)



    override fun onDestroy() {


        try {
            localSocket?.shutdownInput()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        try {
            localSocket?.shutdownOutput()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        try {
            localSocket?.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        try {
            localSocket?.inputStream?.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        try {
            localSocket?.outputStream?.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        localSocket = null
        super.onDestroy()

    }

}